﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.Web.Order
{
    public partial class OrderList2 : BasePage
    {
        public int Status
        {
            get
            {
                return (int)EnumHelper.OrderStatus.CustomerConfirm;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(Page_Load);
            AspNetPager1.PageChanged += new EventHandler(AspNetPager1_PageChanged);
            gvOrderList.RowDataBound += new GridViewRowEventHandler(gvOrderList_RowDataBound);
        }

        void gvOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblStatus = e.Row.FindControl("lblStatus") as Label;
                DataRowView dv = e.Row.DataItem as DataRowView;
                EnumHelper.OrderStatus orderStatus = (EnumHelper.OrderStatus)Convert.ToInt32(dv["OrderStatus"].ToString());
                lblStatus.Text = EnumDescAttribute.GetFieldText(orderStatus);

                Button btnConfirm = e.Row.FindControl("btnConfirm") as Button;
                if (orderStatus == EnumHelper.OrderStatus.CompanyConfirm)
                {
                    btnConfirm.Visible = true;
                }
                else
                    btnConfirm.Visible = false;
            }
        }

        void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            parm = new SqlDbParameter();
            parm.ColumnName = "[OrderStatus]";
            parm.ColumnType = DbType.Int32;
            parm.ParameterName = "@OrderStatus";
            parm.ParameterValue = Status;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            parm = new SqlDbParameter();
            parm.ColumnName = "[CustomerID]";
            parm.ColumnType = DbType.Guid;
            parm.ParameterName = "@CustomerID";
            parm.ParameterValue = base.User.Identity.LoginUser.UserID;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            int RecordCount = 0;
            DataTable dt = new OrderBll().GetOrderList(parms, "OrderDate DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvOrderList.DataSource = dt;
            gvOrderList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }
    }
}